package cn.edu.njnu.opengmpback.collaboration.controller;


import cn.edu.njnu.opengmpback.common.entity.EmailEntity;
import cn.edu.njnu.opengmpback.common.dto.UpdateCollabProjectDTO;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.collaboration.entity.CollabProject;
import cn.edu.njnu.opengmpback.collaboration.service.CollabProjectService;
import cn.edu.njnu.opengmpback.collaboration.enums.CollabProjectPrivacy;
import cn.edu.njnu.opengmpback.collaboration.enums.CollabProjectCategory;
// import cn.edu.njnu.opengmpback.View.StaticPagesBuilder;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
@RestController
@RequestMapping("/application")
public class CollabProjectController {

    private static final Logger logger = LoggerFactory.getLogger(CollabProjectController.class);

    private final CollabProjectService collabProjectService;
    //ProjectService: 业务逻辑处理器。所有关于项目的复杂操作（如数据库查询、权限判断、数据整合）都封装在 ProjectService 中。
    // 这遵循了典型的 Controller-Service 分层架构。
    // private final StaticPagesBuilder staticPagesBuilder;
    // StaticPagesBuilder: 静态页面生成器。这是这个项目混合渲染架构的核心体现。每当一个项目的数据发生改变（例如创建、更新、删除、添加成员），
    // 这个控制器就会调用 staticPagesBuilder 的方法（如 projectListPageBuilder）去重新生成 projectList.html 和 projectDetail.html 等静态页面。

    @Autowired
    public CollabProjectController(CollabProjectService collabProjectService) {
        this.collabProjectService = collabProjectService;
        // this.staticPagesBuilder = new StaticPagesBuilder();
    }

    /**
     * Conditional query
     * @param category
     * @param tag
     * @param keyword
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public JsonResult inquiryByPage(@RequestParam("category") String category,
                                @RequestParam("tag") String tag,
                                @RequestParam("keyword") String keyword,
                                @RequestParam("userId") String userId,
                                @RequestParam("page") int page,
                                @RequestParam("pageSize") int pageSize){
        JsonResult result = collabProjectService.inquiryByConditions(category, tag, keyword, userId, page, pageSize);
        return result;
    }

      /**
     * Create project
     * @param project
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/createCollabProject", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public JsonResult createProject(@RequestBody CollabProject collabProject) throws IOException {
        logger.info("createProject");
        JsonResult result = collabProjectService.createCollabProject(collabProject);

        // Thymeleaf
        // if(result.getCode() == 0){
        //     //将内容同步内容放到这个部分
        //     staticPagesBuilder.projectDetailPageBuilder((Project) result.getData());
        //     staticPagesBuilder.projectListPageBuilder(projectService.findProjectsByPage(1,18));
        // }

        return result;
    }



}
